<template>
    <div>
        <el-table :data="pageResult.records" border style="margin-top: 20px">
            <el-table-column label="id" prop="purchaseId"></el-table-column>
            <el-table-column label="药品" prop="medicinesName"></el-table-column>
            <el-table-column label="数量" prop="purchaseNum"></el-table-column>
            <el-table-column label="单位" prop="purchaseUnit"></el-table-column>
            <el-table-column label="批发价" prop="purchaseTradePrice"></el-table-column>
            <el-table-column label="处方价" prop="purchasePrescriptionPrice"></el-table-column>
            <el-table-column label="批发额" prop="purchaseTradeTotalAmount"></el-table-column>
            <el-table-column label="处方额" prop="purchasePrescriptionTotalAmount"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="text" @click="changePrice(scope.row)">调价</el-button>
                    <el-button type="text" @click="inventory(scope.row)">入库</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-divider></el-divider>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageResult.current"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageResult.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageResult.total">
        </el-pagination>

        <el-dialog
                title="调整价格"
                :visible.sync="dialogVisible.price"
                width="30%">
            <price ref="price" @submit="handleUpdate"></price>
        </el-dialog>
    </div>
</template>

<script>
    import {PurchaseService} from "../../../model/gs/PurchaseService";

    const price = () => import('./price.vue')

    const purchaseService = PurchaseService.getInstance()

    export default {
        data() {
            return {
                dialogVisible: {
                    price: false,
                },
                searchForm: {
                    page: 1,
                    size: 5
                },
                pageResult: {}
            }
        },
        methods: {
            getPage() {
                purchaseService.getPage(this.searchForm).then(({data}) => {
                    if (data.code === 0) {
                        this.pageResult = data.data
                    }
                })
            },
            //调价
            changePrice(row) {
                this.dialogVisible.price = true
                this.$nextTick(() => {
                    this.$refs.price.init(row, 'purchasePrescriptionPrice')
                })
            },
            //提交调价
            handleUpdate(price, row) {
                row.purchasePrescriptionPrice = price
                row.purchasePrescriptionTotalAmount = row.purchasePrescriptionPrice * row.purchaseNum
                purchaseService.update(row).then(({data}) => {
                    if (data.code === 0) {
                        this.$message.success({
                            message: '调价成功',
                            duration: '1000'
                        })
                        this.dialogVisible.price = false
                        this.getPage()
                    }
                })
            },
            inventory(row) {
                purchaseService.inventory(row.purchaseId).then(({data}) => {
                    if (data.code === 0) {
                        this.$message.success({
                            message: '入库成功',
                            duration: 1000
                        })
                        this.getPage()
                    }
                })
            },
            //size改变的方法
            handleSizeChange(size) {
                this.searchForm.size = size
                this.getPage()
            },
            //当前页改变的方法
            handleCurrentChange(page) {
                this.searchForm.page = page
                this.getPage()
            },
        },
        created() {
            this.getPage()
        },
        components: {
            price
        }
    }
</script>

<style scoped>

</style>
